package com.xh.audit.controller.project;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.project.AuditWorkpaper;
import com.xh.audit.service.AuditWorkpaperService;
import com.xh.audit.vo.webform.AuditWorkpaperQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计底稿;项目关联 审计底稿 ,审计通知,
 * 一个项目多个审计疑点 ; 疑点可以再关联底稿;
 * 一个项目多个审计取证;取证可以再关联底稿
 * 一个项目多个审计问题;问题可以再关联底稿
 * 一个项目多个审计通知 (系统暂时显示一个 不限制);
 * 一个项目多个审计底稿 ;审计底稿页会联动审计疑点 ,审计取证,审计问题
 * 选择项目后回填对应的计划
 * 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/audit-workpaper")
@RequiredArgsConstructor
@Validated
public class AuditWorkpaperController {

    private final AuditWorkpaperService auditWorkpaperService;

    @PostMapping("/save")
    public R<AuditWorkpaper> save(@RequestBody
                                  @Validated
                                          AuditWorkpaper tosave) {
        boolean saveFlag = auditWorkpaperService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(auditWorkpaperService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<AuditWorkpaper>> query(@RequestBody
                                         @Validated
                                                 AuditWorkpaperQuery queryPage) {

        Page<AuditWorkpaper> page1 = auditWorkpaperService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<AuditWorkpaper> info(@PathVariable
                                  @Validated
                                  @NotNull(message = "id不能空")
                                          Integer id) {

        AuditWorkpaper byId = auditWorkpaperService.getById(id);
        return R.success(byId);
    }

    @PostMapping("/del")
    public R<AuditWorkpaper> del(@RequestBody
                                 @Validated
                                 @NotEmpty(message = "ids 集合不能空")
                                         List<Integer> ids) {
        auditWorkpaperService.removeByIds(ids);
        return R.success("删除成功");
    }

}
